package com.twodudesdev.amerikick.learnkarate;

import java.util.List;

import com.twodudesdev.amerikick.learnkarate.DBAdapter.DatabaseHelper;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;

public class VideoActivity extends ListActivity {
	
	public DBAdapter db_LearnKarate = (new DBAdapter(this, DatabaseHelper.DB_NAME, null, DatabaseHelper.DATABASE_VERSION));
	public Integer videoId;
	public Integer intList;
	//private String strLocation;
	public Cursor videoCursor;
	public String[] from;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.video);
		
		/** Logo Image */
        ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
        imageView1.setImageResource(R.drawable.training);
        imageView1.setScaleType(ScaleType.FIT_XY);
        
        setupDatabase();
    	
    	createList();
    }
   /*     
    @Override
	public void onClick(View v) {
    	switch (v.getId()) {
    	case R.id.button1:
    		startVideo("nLjCF1_DC0o");
    		break;
    	}
    }
    */
    private void startVideo(String videoID) {
		 // default youtube app
		 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + videoID));
		 List<ResolveInfo> list = getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
		 if (list.size() == 0) {
		  // default youtube app not present or doesn't conform to the standard we know
		  // use our own activity
			 i = (new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + videoID)));
		 }
		 startActivity(i);
	}
    
    private void setupDatabase() {
		//this.deleteDatabase("com.amerikick.main.db");
    	
    	//addUsers();
    	//addSchools();
	}
    
    private void createList() {
		// Query the Database for the States
    	db_LearnKarate.open();
    	
    	videoCursor = db_LearnKarate.getVidsByCat(1);
    	from = new String[] { "desc","id" };
    	
    	startManagingCursor(videoCursor);
    	
		int[] to = new int[] { R.id.location_text };
		
		// Create a List adaptor bound to the cursor.
		// SimpleListAdaptor is designed for binding to a Cursor.
		ListAdapter videoAdapter = new SimpleCursorAdapter(
				this, 
				R.layout.list_items,
				videoCursor,
				from, 
				to);
		
		setListAdapter (videoAdapter);
	}
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
	    super.onListItemClick(l, v, position, id);

	    Cursor o = (Cursor) this.getListAdapter().getItem(position);
	    //Integer locationID = Integer.parseInt(o.getString(0));
	    String videoDesc = o.getString(1);
	    String videoID = o.getString(2);

	    Toast.makeText(getApplicationContext(), videoDesc,
	    		Toast.LENGTH_SHORT).show();
	    
	    // Get instance of Vibrator from current Context
    	Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    	 
    	// Vibrate for 300 milliseconds
    	vib.vibrate(100);
	    
	    startVideo(videoID);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent i;
		// Handle item selection
	    switch (item.getItemId()) {
	    case R.id.contact_us:
	    	i = (new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amerikick.com/index.php?option=com_aicontactsafe&view=message&layout=message&pf=1&Itemid=55")));
	    	startActivity(i);
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
    }
}
